#!/bin/bash

set -e

if [[ -n "${RF_DEBUG}" ]]; then
    set -x
fi

DIR="$(dirname "$0")"

function usage() {
    echo -n \
"Usage: $(basename "$0")

Run various test suites.
"
}

GIT_COMMIT="${GIT_COMMIT:-latest}"

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
    if [ "${1:-}" = "--help" ]; then
        usage
    else
        echo "Linting Bash scripts"
        docker-compose \
            -f docker-compose.test.yml \
            run --rm --no-deps shellcheck \
            scripts/*

        if [ -f /opt/raster-foundry/jars/batch-assembly.jar ]; then
            echo "Verifying that Batch containers can run the batch jar"
            docker-compose \
                run --rm batch \
                bash -c \
                "java -cp /opt/raster-foundry/jars/batch-assembly.jar com.rasterfoundry.batch.Main healthcheck"
        fi

        echo "Linting Scala source code and running tests"
	TEST_CMD="test"
	if [[ "${GIT_BRANCH}" =~ "feature/" ]]; then
	    TEST_CMD="testQuick"
	fi
	docker-compose \
            -f "${DIR}/../docker-compose.yml" \
            -f "${DIR}/../docker-compose.test.yml" \
            run --rm sbt-test \
            ";scalafmtCheck;scalafmtSbtCheck;scalafix --check;scapegoat;unusedCompileDependenciesTest;undeclaredCompileDependenciesTest;${TEST_CMD};apiIntegrationTest/test"

    fi
fi
